package com.zh.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zh.entity.Business;
import com.zh.entity.QueryLog;
import com.zh.entity.User;
import com.zh.service.BusinessService;
import com.zh.service.QueryLogService;
import com.zh.util.DateUtil;
import com.zh.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * 查询日志表 前端控制器
 * </p>
 *
 * @author wiki
 * @since 2018-03-22
 */
@RestController
@RequestMapping("/queryLog")
@Validated
public class QueryLogController extends BaseController {


    @Autowired
    private QueryLogService queryLogService;

    @Autowired
    private BusinessService businessService;

    @PostMapping("/list")
    public Result list(User user, String searchValue){
        Page<QueryLog> page = getPage();
        page = queryLogService.search(page,user.getBusinessId(),searchValue);
        return Result.success(page);
    }

    @PostMapping("/historyQuery")
    public Result historyQuery(User user){
        Business business = businessService.selectById(user.getBusinessId());


        EntityWrapper ew = new EntityWrapper<QueryLog>();
        ew.eq("business_id",user.getBusinessId());
        ew.eq("create_date", DateUtil.getCaleDateString(-1));

        int lastDayQueryCount = queryLogService.selectCount(ew);

        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("allQueryCount",business.getAllQueryCount());
        result.put("hasCount",business.getHasCount());
        result.put("lastDayQueryCount",lastDayQueryCount);
        return Result.success(result);
    }
	
}
